package it.sarasini.dmp;

import it.sarasini.selectablelist.ArtistListItem;
import it.sarasini.selectablelist.TitleListItem;
import it.sarasini.selectablelist.SelectableAdapter;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class AudioActivity extends Activity implements OnItemClickListener {
	
	private MusicDatabaseAdapter mda;
	
	private ListView artistsList;
	private ListView titlesList;
	private ArrayList<ArtistListItem> artistsAdapter;
	private ArrayList<TitleListItem> titlesAdapter;
	private SelectableAdapter artistsSelectableAdapter; 
	private SelectableAdapter titlesSelectableAdapter; 
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	DebugLog.log("onCreate...");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);     
        
        mda = new MusicDatabaseAdapter(getApplicationContext());
        if(mda.openDatabase())
        	DebugLog.log("MusicDatabaseAdapter creato");
        else
        	DebugLog.log("Errore nell'apertura del database");
        
        DebugLog.log("initComponents...");
        initComponents();
        
        DebugLog.log("reloadDatabase...");
        reloadDatabase();
        
        DebugLog.log("loadSongs...");
        loadSongs();
        
    }
    
    private void initComponents() {
    	
    	artistsList = (ListView) findViewById(R.id.listArtists);
        titlesList = (ListView) findViewById(R.id.listTitles);
 
        artistsAdapter = new ArrayList<ArtistListItem>();
        titlesAdapter = new ArrayList<TitleListItem>();
        
        artistsSelectableAdapter = new SelectableAdapter (		
        	getApplicationContext(), 						// The context where the View associated with this SimpleAdapter is running
        	R.layout.lista, 								// Resource identifier of a view layout
        	artistsAdapter									// Data to display
        );
        
        titlesSelectableAdapter = new SelectableAdapter (		
	        getApplicationContext(), 						// The context where the View associated with this SimpleAdapter is running
	        R.layout.lista, 								// Resource identifier of a view layout
	        titlesAdapter									// Data to display
	    );
 
        artistsList.setAdapter(artistsSelectableAdapter);
        titlesList.setAdapter(titlesSelectableAdapter);
        
        artistsList.setOnItemClickListener(this);
        titlesList.setOnItemClickListener(this);
    	
    }
    
    private boolean loadSongs() {
        
        if(mda.openDatabase()) {
        	
        	DebugLog.log("Database aperto");
        	
        	loadArtists();
        	
        	if(!artistsAdapter.isEmpty()) {
        		
        		artistsSelectableAdapter.selectFirst();
        		loadTitles(artistsAdapter.get(0).getId());
        	}
        	else
        		DebugLog.log("Nessun artista");
        }
        else {
        	
        	DebugLog.log("Errore nell'apertura del database");
        	return false;
        }
        
        return true;
    }
    
    private void loadArtists() {
    	
    	ArrayList<ArtistData> artists = mda.getArtists();
    	
    	for(int i=0; i<artists.size(); i++) {
        	
    		DebugLog.log(i + ") Artist, " + artists.get(i).getId() + " " + artists.get(i).getName());
    		
    		artistsAdapter.add(new ArtistListItem(artists.get(i).getName(), artists.get(i).getId()));
    		
    		artistsSelectableAdapter.notifyDataSetChanged();
    	}
    }
    
    private void loadTitles(int artistId) {
    	
    	ArrayList<FileData> files = mda.getFilesFromArtistId(artistId);
    	
    	titlesAdapter.clear();
    	
    	for(int i=0; i<files.size(); i++) {
    		
        	DebugLog.log(i + ") File, " + files.get(i).getId() + " " + files.get(i).getTitle() + " " + files.get(i).getPath());
        	
        	titlesAdapter.add(new TitleListItem(files.get(i).getTitle(), files.get(i).getPath(), files.get(i).getId()));
        	
        	titlesSelectableAdapter.notifyDataSetChanged();
    	}
    	
    	titlesSelectableAdapter.selectFirst();
    }
    
    @Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		
    	DebugLog.log("onItemClick");
    	
    	if(parent.equals(artistsList)) {
    		
    		DebugLog.log("artistsList clicked");
    		
    		artistsSelectableAdapter.select(position);
    		
    		loadTitles(artistsAdapter.get(position).getId());
    	}
    	else if(parent.equals(titlesList)) {
    		
    		DebugLog.log("titlesList clicked");
    		
    		titlesSelectableAdapter.select(position);
    		
    		Toast.makeText(getApplicationContext(), titlesAdapter.get(position).getPath(), Toast.LENGTH_SHORT).show();
    	}
	}
    
    private boolean reloadDatabase() {
    	
    	FileManager fm = new FileManager();

    	fm.addFileOrDirectory("/mnt/sdcard/musica/");

    	ArrayList<File> files = fm.getAllFiles();

    	if(files.isEmpty()) {
    		
    		DebugLog.log("Nessun file trovato.");
    		return false;
    	}

    	SongInfo songInfo;
    	for(int i=0; i<files.size(); i++) {
    		
    		DebugLog.log(i + ") " + files.get(i).getPath());
    		
    		songInfo = new SongInfo(getApplicationContext(), files.get(i));
    	    
    		DebugLog.log(i + ") " + songInfo.getArtist());
    		DebugLog.log(i + ") " + songInfo.getTitle());
    		DebugLog.log(i + ") " + songInfo.getAlbum());

    		DebugLog.log("---------------------------------------");
    		
    		mda.addNewFile(files.get(i).getPath(), songInfo.getArtist(), songInfo.getTitle(), songInfo.getLength());
    	    
    		songInfo = null;
    	}
    	
    	return true;
    }
}

